package adminLoginPack;

import java.util.List;
import InvestmentPojo.userInformationPojo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import InvestmentPojo.userTablePojo;
import Notifications.notificationsDataLoading;

public class userLoginAction extends Action {
   
	/*
	 * (non-Javadoc) Returns login action.
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
 
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
 
       String mappingValue  ="";
       String userName = "";
       String password = "";
       String method = "";
       String addUsrMethod = "";
       boolean admin = false;
       boolean user = false;
       /*
        * re
        */
       try{
           password = (String)request.getParameter("password");
           method = (String)request.getParameter("method123");
           
           String adminValue = null != request.getParameter("admin")?(String)request.getParameter("admin"):"N";
           userName = null != request.getParameter("userId")?(String)request.getParameter("userId"):"N";
           
           addUsrMethod= (String)request.getParameter("METHOD1234");
           if(null == method && "".equals(method))
           method = null != request.getParameter("a")?request.getParameter("a"):"";
           
           String mainmethod123 = null != request.getParameter("mainmethod123")?(String)request.getParameter("mainmethod123"):"N";
           
		   if(null != method && method.equals("submit")){
			   
			  
			   notificationsDataLoading dataLoading = new notificationsDataLoading();
			   Encryption enc = new Encryption();
		       String encoding = enc.passwordEncryption(password);
		       if(!"".equals(adminValue) && adminValue.equals("A") ){
		    	   admin = true;
		       }else if(!"".equals(adminValue) && adminValue.equals("U")){
		    	   user = true;
		       }
		    	   
		       boolean flag = dataLoading.isAuthenticated(userName, encoding,admin);
		       
		       
			       if(flag && admin){
			    	   
			    	   HttpSession session = request.getSession();
			    	   mappingValue ="adminSuccess";
			    	   request.setAttribute("UserID", userName);
			           request.getSession().setAttribute("UserID", userName);
			    	   request.setAttribute("UserID1", userName);
			    	   request.setAttribute("serviceId","1") ;
			       }else if(flag) {
			    	  
			    	   mappingValue ="success";
			    	   request.setAttribute("UserID", userName);
			           request.getSession().setAttribute("UserID", userName);
			    	   request.setAttribute("UserID1", userName);
			    	   request.setAttribute("serviceId","1") ;
			    	   if(mainmethod123.equals("mainPageLogin")){
			    		   request.setAttribute("UserID", "");
				           request.getSession().setAttribute("UserID", "");
				    	   request.setAttribute("UserID1", "");
				    	   request.setAttribute("serviceId","0") ;
			    	   }
			       }else{
			    	   mappingValue ="failure";
			    	   request.setAttribute("authFailed", "authFailed");
			       }
			       
			       List userList = dataLoading.getUserData(userName, "investment_user1");
		    	   if(null != userList && !userList.isEmpty()){
		    		   userInformationPojo userPojo = (userInformationPojo)userList.get(0);
		    		   String isAdmin = userPojo.getAdminFlag();
		    		   String userNameforDisplay = userPojo.getUserFirstName()+" "+userPojo.getUserMiddleName();
		    		   
		    		   request.getSession().setAttribute("isAdmin", isAdmin);
		    		   request.getSession().setAttribute("DisplayName", userNameforDisplay);
		    	   }
		   }else if(null != method && method.equals("logout")){
    	   request.removeAttribute("UserID");
    	   request.getSession().invalidate();
    	   System.out.println("Session closed and user id is------------>"+request.getAttribute("UserID")+" <br>And User name--->"+userName);
    	   mappingValue = "authfailiure";
    	  
       }else  if(null != method && method.equals("newReg")){
    	   
    	   String loginUserPage = "UserPage";
    	   request.setAttribute("loginUserPage", loginUserPage);
    	  
    	   mappingValue ="success";
       }else if(null != method && method.equals("admin")){
    	   
    	   String loginUserPage = "UserPage";
    	   request.setAttribute("loginUserPage", loginUserPage);
    	  
    	   mappingValue ="adminSuccess";    	   
       }else{
    	   mappingValue ="failure";
       }
       if(null != addUsrMethod && addUsrMethod.equals("AddUser")){
    	   
    	   String username = null != (String)request.getParameter("username")?(String)request.getParameter("username"):"";
           String userId = null != (String)request.getParameter("userIdforIns")?(String)request.getParameter("userIdforIns"):"";
           String pwd = null != (String)request.getParameter("pwd")?(String)request.getParameter("pwd"):"";
           
           Encryption enc = new Encryption();
           String encoding = enc.passwordEncryption(pwd);
           
           String surname = null != (String)request.getParameter("surname")?(String)request.getParameter("surname"):"";
           String names = null != (String)request.getParameter("names")?(String)request.getParameter("names"):"";
           
           String dob = null != (String)request.getParameter("dob")?(String)request.getParameter("dob"):"";
           String email = null != (String)request.getParameter("email")?(String)request.getParameter("email"):"";
           String tel = null != (String)request.getParameter("tel")?(String)request.getParameter("tel"):"";
           
           String userAddress = null != (String)request.getParameter("userAddress")?(String)request.getParameter("userAddress"):"";
           String ptc = null != (String)request.getParameter("ptc")?(String)request.getParameter("ptc"):"";
           
           String userType = null != (String)request.getParameter("registor")?(String)request.getParameter("registor"):"";
           
           if(!"".equals(adminValue) && adminValue.equals("Y") )
        	   admin = true;
           
           userTablePojo utp = new userTablePojo();
           utp.setUsername(username);
           utp.setUserId(userId);
           utp.setPwd(encoding);
           utp.setSurname(surname);
           utp.setDatepicker(dob);
           utp.setEmail(email);
           utp.setTel(tel);
           utp.setUserAddress(userAddress);
           utp.setPtc(ptc);
           utp.setAdmin(admin);
           utp.setUserType(userType);
           notificationsDataLoading dataLoading = new notificationsDataLoading();
           boolean flag = dataLoading.setUserInformation(utp);
           request.setAttribute("insertedFlag", flag);
           
           mappingValue = "jsp";
    	   
           mappingValue = "success";
       }
       }catch(Exception e){
    	   e.printStackTrace();
       }
       
      
       
       
       return mapping.findForward(mappingValue);
 
    }
 
}


